import React from 'react';
import { Divider } from 'antd';
import moment from 'moment';
import { Icon } from '@/pages/Recruitment/components';
import { newTabProps } from '@/utils/tab';
import { TRIAL_ACTION, TRIAL_ACTION_LIST, TRIAL_STATUS, TRIAL_STATUS_LABEL_LIST, STATUS_ACTION } from '../constants';

const READY_LIST = [
    TRIAL_ACTION.INTERVIEW_READY_WEEK,
    TRIAL_ACTION.INTERVIEW_READY_MONTH,
    TRIAL_ACTION.INTERVIEW_READY_QUARTER
];

const SUMMARY_LIST = [
    TRIAL_ACTION.INTERVIEW_SUMMARY_WEEK,
    TRIAL_ACTION.INTERVIEW_SUMMARY_MONTH,
    TRIAL_ACTION.INTERVIEW_SUMMARY_QUARTER
];

// 列表操作业务权限判断 待完善逻辑
export const renderActionIcon = (record, user, onClick) => {
    const { status, operateType, initiatorCode, wasPrepareOperateType, employeeCode } = record;
    // 判断是否有操作权限
    if (!record.inProcess) return null;

    // 排除当前面谈人的面谈状态按钮
    if (employeeCode === user?.employeeCode) {
        return null;
    }

    // 判断是否在对应的状态列表中
    const obj = TRIAL_ACTION_LIST.find(item => item.value === status);
    if (!obj) return null;

    const { title, icon } = obj;
    if (!icon) return null;

    // 面谈准备状态
    if (READY_LIST.includes(status)) {
        // 已提交过准备
        if (operateType === 1) {
            return (
                <>
                    <Icon
                        title="面谈记录"
                        type="miantanjilu"
                        onClick={() => onClick(record, status ? status.replace('Ready', 'Log') : status)}
                    />
                    <Divider type="vertical" />
                    <Icon title="修改面谈准备" type="miantanzhunbei" onClick={() => onClick(record, status)} />
                </>
            );
        }
        // 未提交准备
        if (!operateType) {
            if (wasPrepareOperateType) {
                return (
                    <>
                        <Icon title="面谈准备" type="miantanzhunbei" onClick={() => onClick(record, status)} />
                        <Divider type="vertical" />
                        <Icon
                            title="面谈记录"
                            type="miantanjilu"
                            onClick={() => onClick(record, status ? status.replace('Ready', 'Log') : status)}
                        />
                    </>
                );
            }
            return <Icon title="面谈准备" type="miantanzhunbei" onClick={() => onClick(record, status)} />;
        }
    }

    // 面谈总结状态
    if (SUMMARY_LIST.includes(status)) {
        let recordIcon = null;
        let summaryIcon = null;
        let detailIcon = null;
        // 未提交过面谈记录
        if (!operateType || operateType === 1) {
            recordIcon = (
                <Icon
                    title="面谈记录"
                    type="miantanjilu"
                    onClick={() => onClick(record, status ? status.replace('Summary', 'Log') : status)}
                />
            );
        }
        // 发起人
        if (user && initiatorCode && initiatorCode === user?.employeeCode) {
            summaryIcon = <Icon title="面谈总结" type="miantanzongjie" onClick={() => onClick(record, status)} />;
        }
        // 已提交过面谈记录
        if (operateType === 2) {
            detailIcon = (
                <Icon
                    title="面谈详情"
                    type="miantanxiangqing"
                    onClick={() => onClick(record, status ? status.replace('Summary', 'Log') : status, newTabProps)}
                />
            );
        }
        const arr = [recordIcon, summaryIcon, detailIcon].filter(item => item);
        return (
            <>
                {arr.map((item, index) => (
                    <>
                        {item}
                        {index < arr.length - 1 && <Divider type="vertical" />}
                    </>
                ))}
            </>
        );
    }

    // 罪过
    let clickProps = [record];
    if (title === '面谈详情') {
        clickProps = [...clickProps, null, newTabProps];
    }

    return <Icon title={title || ''} type={icon || ''} onClick={() => onClick(...clickProps)} />;
};

// 渲染“面谈待记录”状态的逻辑
export const getInterviewTip = (record, user) => {
    const {
        status,
        operateType,
        wasPrepareOperateType,
        assignEmployeeCodes,
        employeeCode,
        interviewStartTime
    } = record;
    // 面谈准备状态
    if (READY_LIST.includes(status)) {
        // 面谈时间到，无论有没有操作过准备，都是面谈待记录状态
        if (moment().isAfter(interviewStartTime)) {
            return '面谈待记录';
        }
        // 判断当前记录不是自己的，并且自己是面谈参与人
        if (employeeCode !== user?.employeeCode && assignEmployeeCodes.includes(user?.employeeCode)) {
            // 已提交过准备
            if (operateType === 1) {
                return '面谈待记录';
            }
            return '';
        }
        // 非面谈参与人（高层）按最新状态来渲染
        if (wasPrepareOperateType) {
            return '面谈待记录';
        }
        return '';
    }
    return '';
};

export const formatStatus = status => {
    let type = '';
    if (STATUS_ACTION[TRIAL_STATUS.GUIDE].includes(status)) {
        type = TRIAL_STATUS.GUIDE;
    } else if (STATUS_ACTION[TRIAL_STATUS.ASSESS_WEEK].includes(status)) {
        type = TRIAL_STATUS.ASSESS_WEEK;
    } else if (STATUS_ACTION[TRIAL_STATUS.ASSESS_MONTH].includes(status)) {
        type = TRIAL_STATUS.ASSESS_MONTH;
    } else if (STATUS_ACTION[TRIAL_STATUS.ASSESS_QUARTER].includes(status)) {
        type = TRIAL_STATUS.ASSESS_QUARTER;
    } else if (STATUS_ACTION[TRIAL_STATUS.INTERVIEW_WEEK].includes(status)) {
        type = TRIAL_STATUS.INTERVIEW_WEEK;
    } else if (STATUS_ACTION[TRIAL_STATUS.INTERVIEW_MONTH].includes(status)) {
        type = TRIAL_STATUS.INTERVIEW_MONTH;
    } else if (STATUS_ACTION[TRIAL_STATUS.INTERVIEW_QUARTER].includes(status)) {
        type = TRIAL_STATUS.INTERVIEW_QUARTER;
    } else if (STATUS_ACTION[TRIAL_STATUS.TRIAL_DONE].includes(status)) {
        type = TRIAL_STATUS.TRIAL_DONE;
    } else if (STATUS_ACTION[TRIAL_STATUS.ASSESSING].includes(status)) {
        type = TRIAL_STATUS.ASSESSING;
    }
    return TRIAL_STATUS_LABEL_LIST[type];
};

export const getStatusList = arr => {
    if (arr && arr.length > 0) {
        return arr.reduce((pre, cur) => [...pre, ...(STATUS_ACTION[cur] || [])], []);
    }
    return [];
};

/**
 * 便于调试，用于快捷修改是否必填
 * @param {*} message
 */
export const requireRule = message => [
    {
        required: true,
        message
    }
];

export const formatAssessLevel = (levels, title) => {
    if (levels && levels.length > 0 && title) {
        let temp = 3;
        let flag = false;
        for (let index = 0; index < levels.length; index += 1) {
            const elem = levels[index];
            if (title === elem?.level) {
                flag = true;
                break;
            }
            if (elem?.entry) {
                temp -= 1;
            }
            if (elem?.qualified) {
                temp -= 1;
            }
        }
        return flag ? temp : '';
    }
    return '';
};
